package com.badibadi.adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.toolbox.NetworkImageView;
import com.badibadi.infos.Img;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.VolleyTools;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class Gridview3Adapter extends BaseAdapter {

	private DisplayImageOptions options;
	private List<Img> list;
	private Context context;

	public Gridview3Adapter(Context ct, List<Img> list) {
		context = ct;
		this.list = list;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.net_img1_img, null);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (list.get(position).getPath().indexOf(Constants.BadiDownImgUrl) != -1) {
			try {
//				ImageLoader.getInstance().displayImage(
//						list.get(position).getPath() + Constants.App9block,
//						holder.img, options);
				new BitmapHelp().displayImage(context, holder.img, list.get(position).getPath() + Constants.App9block);
				// VolleyTools.displayImageByNetworkImageView(context,
				// Constants.BadiDownImgUrl+"/"+list.get(position).getPath(),
				// holder.networkImageView);
			} catch (Exception e) {
				// TODO: handle exception
			}

		} else if (list.get(position).getPath()
				.indexOf(Constants.BadiWebImgUrl) != -1) {
//			ImageLoader.getInstance().displayImage(
//					list.get(position).getPath(), holder.img, options);
			new BitmapHelp().displayImage(context, holder.img, list.get(position).getPath());
		} else if (list.get(position).getPath()
				.indexOf(Constants.Fei_biaoqing_qianzui4) != -1) {
//			ImageLoader.getInstance().displayImage(
//					Constants.BadiWebImgUrl + list.get(position).getPath(),
//					holder.img, options);
			new BitmapHelp().displayImage(context, holder.img, Constants.BadiWebImgUrl + list.get(position).getPath());
		} else {
			try {
//				ImageLoader.getInstance().displayImage(
//						Constants.BadiDownImgUrl + "/"
//								+ list.get(position).getPath()
//								+ Constants.App9block, holder.img, options);
				new BitmapHelp().displayImage(context, holder.img,
						Constants.BadiDownImgUrl + "/"
								+ list.get(position).getPath()
								+ Constants.App9block);

				// VolleyTools.displayImageByNetworkImageView(context,
				// Constants.BadiDownImgUrl+"/"+list.get(position).getPath(),
				// holder.networkImageView);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return convertView;
	}

	static class ViewHolder {
		public ImageView img;
		public ImageView networkImageView;
	}

	public List<String> getSList() {
		List<String> mList;
		mList = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getPath().indexOf(Constants.BadiDownImgUrl) != -1 || list.get(i).getPath().indexOf(Constants.Fei_biaoqing_qianzui4)!=-1 || list.get(i).getPath().indexOf(Constants.Fei_biaoqing_qianzui3)!=-1) {
				mList.add(list.get(i).getPath());
			}

		}
		return mList;
	}

	public List<Img> getList() {
		return list;
	}

	public void setList(List<Img> list) {
		this.list = list;
	}

}
